<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/8/19
 * Time: 15:07
 */

namespace app\api\controller;


use think\Db;

class Timing extends Base
{
//    public function aaa()
//    {
//        set_time_limit(0);
//        $today = date('Y-m-d H:i:s');
//        $data = Db::table('up_zzz')
//            ->where('id', 'between','794,900')
////            ->whereOr('id', 'between','513,714')
//            ->select();
////        dump($data);die;
//        $first = '';
//        $res = [];
//        $res2 = [];
//        $i = 0;
//        foreach ($data as $k => $v){
//            $arr = explode('(',trim($v['remark'],' '));
////            if($arr[1] == 'Air' || $arr[2] == 'mini') $arr[1] = $arr[1] . ' ' . $arr[2];
//            $arr[0] = trim($arr[0],' ');
//            if($arr[0] != $first){
//                $first = $arr[0];
//                $res[$k] = [
//                    'models_brand_id' => 3,
//                    'models_cate_id' => 2,
//                    'models_name' => $arr[0],
//                    'models_all_name' => ','.$v['name'].',',
//                    'models_time' => $today,
//                    'models_update_time' => $today
//                ];
//                $res2[$k] = [
//                    'mn_name' => ','.$v['name'].',',
//                    'mn_time' => $today,
//                    'mn_update_time' => $today
//                ];
//                $i = 0;
//            }else{
//                $i++;
//                $res[$k-$i]['models_all_name'] .= $v['name'].',';
//                $res2[$k-$i]['mn_name'] .= $v['name'].',';
//            }
//        }
//        dump($res);die;
//        $a = Db::table('up_models')->insertAll($res);
//        $b = Db::table('up_model_name')->insertAll($res2);
//        dump($a);
//        dump($b);
//        die;
//    }
//    public function aaa2()
//    {
//        set_time_limit(0);
//        $today = date('Y-m-d H:i:s');
//        $data = Db::table('up_zzz')
//            ->where('id', 'between','2093,3092')
////            ->whereOr('id', 'between','513,714')
//            ->select();
////        dump($data);die;
//        $first = '';
//        $res = [];
//        $res2 = [];
//        $i = 0;
//        foreach ($data as $k => $v){
//            $arr = explode(' ',trim($v['remark'],' '));
//            unset($arr[count($arr)-1]);
//            $arr[1] = implode($arr,' ');
////            if($arr[1] == 'Air' || $arr[2] == 'mini') $arr[1] = $arr[1] . ' ' . $arr[2];
////            dump($k);
//            if($arr[1] != $first){
//                $first = $arr[1];
//                $res[$k] = [
//                    'models_brand_id' => 6,
//                    'models_cate_id' => 2,
//                    'models_name' => $arr[1],
//                    'models_all_name' => ','.$v['name'].',',
//                    'models_time' => $today,
//                    'models_update_time' => $today
//                ];
//                $res2[$k] = [
//                    'mn_name' => ','.$v['name'].',',
//                    'mn_time' => $today,
//                    'mn_update_time' => $today
//                ];
//                $i = 0;
//            }else{
//                $i++;
//                $res[$k-$i]['models_all_name'] .= $v['name'].',';
//                $res2[$k-$i]['mn_name'] .= $v['name'].',';
//            }
//        }
//        dump($res);die;
//        $a = Db::table('up_models')->insertAll($res);
//        $b = Db::table('up_model_name')->insertAll($res2);
//        dump($a);
//        dump($b);
//        die;
//    }
    /**
     * 寄存手机订单，寄存天数倒计时
     * 如果当前寄存剩余天数为0，则修改状态为逾期
     * 每天凌晨跑
     */
    public function decDays(){
        $order = Db::table('up_order')
            ->where('order_type', 2)
            ->where('order_status', 'IN', [2,3])
            ->field('order_id, order_jc_sy_days')
            ->select();
        if($order){
            foreach($order as $k => $v){
                $update['order_jc_sy_days'] = $v['order_jc_sy_days'] - 1;
                if($v['order_jc_sy_days'] == 0){
                    $update['order_status'] = 6;
                }
                Db::table('up_order')->where('order_id', $v['order_id'])->update($update);
            }
        }
    }
    
    /**
     * 申请5天内没验机的，改为已取消状态
     * 每分钟跑
     */
    public function changeOrderCancel(){
        $five_days = date('Y-m-d H:i:s', strtotime('-5days'));
        Db::table('up_order')
            ->where('order_status', 1)
            ->where('order_time', '<', $five_days)
            ->update(['order_status' => 5, 'order_cancel_reason' => '申请5天内没去验机']);
    }
}